package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.EmployeeMapper;
import com.hrma.dao.RecruitScheduleMapper;
import com.hrma.dao.TalentPoolMapper;
import com.hrma.entity.*;
import com.hrma.entity.exception.StateException;
import com.hrma.services.RecruitScheduleService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecruitScheduleImpl implements RecruitScheduleService {
    private final RecruitScheduleMapper recruitScheduleMapper;
    private final TalentPoolMapper talentPoolMapper;
    private final EmployeeMapper employeeMapper;

    public RecruitScheduleImpl(RecruitScheduleMapper recruitScheduleMapper, TalentPoolMapper talentPoolMapper, EmployeeMapper employeeMapper) {
        this.recruitScheduleMapper = recruitScheduleMapper;
        this.talentPoolMapper = talentPoolMapper;
        this.employeeMapper = employeeMapper;
    }

//    @Override
//    public RecruitSchedule add(RecruitSchedule data) {
//        if (!recruitScheduleMapper.exist(data.getId())) {
//            throw new RuntimeException("id字段不存在！");
//        }
//        recruitScheduleMapper.add(data);
//        return data;
//    }

    @Override
    public void delete(RecruitScheduleVO data) {
        if (!recruitScheduleMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        if (!talentPoolMapper.exist(data.getTalentPoolId())) {
            throw new RuntimeException("id字段不存在！");
        }
        recruitScheduleMapper.delete(data.getId());
        talentPoolMapper.setState(data.getState(), data.getTalentPoolId());
    }

    @Override
    public void update(RecruitScheduleVO data) {
        if (!talentPoolMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        if (data.getState() >= 4) {
            throw new StateException("考核已结束，请决定是否录取！");
        }
        talentPoolMapper.setState(data.getState(), data.getId());
    }

    @Override
    public List<SelectResult> select() {
        return recruitScheduleMapper.select();
    }

    @Override
    public Page<RecruitScheduleVO> getList(RecruitScheduleDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return recruitScheduleMapper.getList(data);
    }

    @Override
    public EmployeeFromRecruit hire(EmployeeFromRecruit data) {
        if (!recruitScheduleMapper.exist(data.getRecruitId())) {
            throw new RuntimeException("id字段不存在！");
        }
        if (!talentPoolMapper.exist(data.getTalentPoolId())) {
            throw new RuntimeException("id字段不存在！");
        }
        recruitScheduleMapper.delete(data.getRecruitId());
        talentPoolMapper.setState(data.getRecruitState(), data.getTalentPoolId());
        employeeMapper.add(data);
        return data;
    }
}
